/***********************************************************************
 main.m - Main module of the WickDD QuickTime movie assembler.

 Created 2006.06.18 by Warren Young

 Copyright (c) 2006 by Pixel Corps.  This program may be used under
 the terms of the X11 license, a copy of which should have accompanied
 this program, in the LICENSE file.
***********************************************************************/

#include "qtvideo.h"
#include "qttimecode.h"

#import <Cocoa/Cocoa.h>

#include <getopt.h>


//// Usage /////////////////////////////////////////////////////////////

static void
Usage()
{
	fprintf(stderr, "usage: wickqtassy [-f filespec] [-m movie]\n\n");
	fprintf(stderr, "    filespec is a glob pattern for the frames "
			"(e.g. \"*.tiff\")\n");
	fprintf(stderr, "    movie is the name of the H.264 movie to "
			"write out.\n\n");
	fprintf(stderr, "Both -f and -m must be given.\n\n");
}


//// ParseCommandLine //////////////////////////////////////////////////

static BOOL
ParseCommandLine(int argc, char* const argv[],
		NSString** ppFileSpec, NSString** ppMovieFileName)
{
	int ch;
	while ((ch = getopt(argc, argv, "f:m:")) != EOF) {
		switch (ch) {
			case 'f':
				*ppFileSpec = [NSString stringWithUTF8String:optarg];
				break;
				
			case 'm':
				*ppMovieFileName =
						[NSString stringWithUTF8String:optarg];
				break;
				
			default:
				Usage();
				return NO;
		}
	}
	
	if (*ppFileSpec && *ppMovieFileName) {
		return YES;
	}
	else {
		Usage();
		return NO;
	}
}


//// main //////////////////////////////////////////////////////////////

int
main(int argc, char* const argv[])
{
	NSAutoreleasePool* autoreleasepool =
			[[NSAutoreleasePool alloc] init];

	BOOL success = NO;
	@try {
		QTMovie* movie;
		NSString* fileSpec;
		NSString* movieFileName;
		if (ParseCommandLine(argc, argv, &fileSpec, &movieFileName) &&
				CreateVideoTrack(&movie, movieFileName, fileSpec) &&
				AddTimecodeTrackToMovie(movie) &&
				CloseMovie()) {
			success = YES;
		}
	}
	@catch (NSException* err) {
		NSLog(@"Caught %@: %@", [err name], [err reason]);
	}

	[autoreleasepool release];	

    return success ? 0 : 1;
}
